package logic

import (
	"context"
	"sport/models/mysql"

	"sport/sportrpc/internal/svc"
	"sport/sportrpc/sportrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type PostListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewPostListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PostListLogic {
	return &PostListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 帖子列表
func (l *PostListLogic) PostList(in *sportrpc.PostListRequest) (*sportrpc.PostListResponse, error) {
	// todo: add your logic here and delete this line
	offset := (in.Page - 1) * in.Size
	var post []mysql.Post
	mysql.DB.Where("id = ?", in.ForumId).Offset(int(offset)).
		Limit(int(in.Size)).Order("liked desc").Find(&post)
	var posts []*sportrpc.Lists
	for _, m := range post {
		list := sportrpc.Lists{
			UserId:  int64(m.UserId),
			ForumId: int64(m.ForumId),
			Title:   m.Title,
			Content: m.Content,
			Liked:   int64(m.Liked),
		}
		posts = append(posts, &list)
	}
	return &sportrpc.PostListResponse{
		List: posts,
	}, nil
}
